package com.tencreat.common.utils;

import com.tencreat.common.core.domain.OrderVO;
import com.tencreat.common.core.domain.dto.AfterSaleDTO;
import com.tencreat.common.enums.*;
import com.tencreat.common.utils.bean.ActionMessage;
import com.tencreat.common.utils.bean.UserBalanceChange;
import com.tencreat.common.utils.bean.UserIntegralChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;

/**
 * 描述信息
 *
 * @Author lx
 * @Company: 深圳腾创互联科技有限公司
 * @link: https://tencreat.com
 * @Date: 2022/7/20 10:35
 */

@Component
@Slf4j
public class RabbitSendUtils {
    @Resource
    private RabbitTemplate rabbitTemplate;

    /*余额变动*/
    public void sendBalanceMessage(Long uuid, BigDecimal amount, String type, BalanceBusinessType use, String remark, String source,BalanceTypeEnum balanceType) {
        log.info("发送余额变动消息----------UUID：{},type:{},amount:{},use:{}，remark：{}-------",uuid,type,amount,use.getDesc(),remark);
        if(amount == null || amount.compareTo(BigDecimal.ZERO) == 0){
            log.info("余额为0，不发放----------UUID：{},type:{},amount:{},use:{}，remark：{}-------",uuid,type,amount,use.getDesc(),remark);
            return;
        }
        //修改用户余额
        UserBalanceChange message = new UserBalanceChange();
        message.setUuid(uuid);
        message.setChangAmount(amount);
        message.setType(type);
        message.setBusinessType(use);
        message.setRemark(remark);
        message.setSource(source);
        message.setBalanceType(balanceType);
        Random random = new Random();
        int delayTimes = random.nextInt(500);
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(QueueEnum.BALANCE_CHANGE.getExchange(),  QueueEnum.BALANCE_CHANGE.getRouteKey(), message, a ->{
            a.getMessageProperties().setDelay(Math.toIntExact(delayTimes));
            return a;
        });
    }


    /**
     * 描述信息   消费券变动
     * @Author yaoruiqi
     * @Date: 2023/10/08 06:27
     */
    public void sendConsumerRollMessage(Long uuid, BigDecimal amount, String type, BalanceBusinessType use, String remark, String source,BalanceTypeEnum balanceType) {
        log.info("发送消费券变动消息----------UUID：{},type:{},amount:{},use:{}，auctionPay：{}，remark：{}-------",uuid,type,amount,use.getDesc(),remark);
        if(amount == null || amount.compareTo(BigDecimal.ZERO) == 0){
            return;
        }
        //修改用户消费券
        UserBalanceChange message = new UserBalanceChange();
        message.setUuid(uuid);
        message.setChangAmount(amount);
        message.setType(type);
        message.setBusinessType(use);
        message.setRemark(remark);
        message.setSource(source);
        message.setBalanceType(balanceType);
        // 发送消费券变动消息
        rabbitTemplate.convertAndSend(QueueEnum.ConsumerRoll_CHANGE.getExchange(), QueueEnum.ConsumerRoll_CHANGE.getRouteKey(), message);
    }

    public void sendGoodsRollMessage(Long uuid, BigDecimal amount, String type, BalanceBusinessType use, String remark, String source,BalanceTypeEnum balanceType) {
        log.info("发送提货单变动消息----------UUID：{},type:{},amount:{},use:{}，auctionPay：{}，remark：{}-------",uuid,type,amount,use.getDesc(),remark);
        if(amount == null || amount.compareTo(BigDecimal.ZERO) == 0){
            return;
        }
        //修改用户余额
        UserBalanceChange message = new UserBalanceChange();
        message.setUuid(uuid);
        message.setChangAmount(amount);
        message.setType(type);
        message.setBusinessType(use);
        message.setRemark(remark);
        message.setSource(source);
        message.setBalanceType(balanceType);
        // 发送余额变动消息
        rabbitTemplate.convertAndSend(QueueEnum.BALANCE_CHANGE.getExchange(), QueueEnum.BALANCE_CHANGE.getRouteKey(), message);
    }

    public void sendActionMessage(Long uuid, ActionMessageType actionMessageType, Object data) {
        log.info("发送业务通知----------UUID：{}-------",uuid);
        ActionMessage actionMessage = new ActionMessage();
        actionMessage.setUuid(uuid);
        actionMessage.setType(actionMessageType);
        actionMessage.setData(data);
        // 发送余额变动消息
        rabbitTemplate.convertAndSend(QueueEnum.ACTION_MESSAGE.getExchange(), QueueEnum.ACTION_MESSAGE.getRouteKey(), actionMessage);
    }

    public void sendMessageAfterSaleBack(AfterSaleDTO dto, final long delayTimes){
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(QueueEnum.QUEUE_DELAYED_AFTER_SALE_BACK.getExchange(),  QueueEnum.QUEUE_DELAYED_AFTER_SALE_BACK.getRouteKey(), dto, a ->{
            a.getMessageProperties().setDelay(Math.toIntExact(delayTimes));
            return a;
        });
        log.info("send delay afterSale back confirm orderSn:{}",dto.getOrderGoodsId());
    }
    public void sendMessageAfterSale(AfterSaleDTO dto, final long delayTimes){
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(QueueEnum.QUEUE_DELAYED_AFTER_SALE_CONFIRM.getExchange(),  QueueEnum.QUEUE_DELAYED_AFTER_SALE_CONFIRM.getRouteKey(), dto, a ->{
            a.getMessageProperties().setDelay(Math.toIntExact(delayTimes));
            return a;
        });
        log.info("send delay afterSale confirm orderSn:{}",dto.getOrderGoodsId());
    }

    /****
     *
     * @param orderVO 包含订单ID以及订单类型
     * @param delayTimes
     */
    public void sendMessage(OrderVO orderVO, final long delayTimes){
        log.info("发送订单取消延迟队列,orderSn:{}",orderVO.getOrderSn());
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(QueueEnum.QUEUE_DELAYED_ORDER_CANCEL.getExchange(),  QueueEnum.QUEUE_DELAYED_ORDER_CANCEL.getRouteKey(), orderVO, a ->{
            a.getMessageProperties().setDelay(Math.toIntExact(delayTimes));
            return a;
        });
    }
}
